//package com.itheima.project.config;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.http.Header;
//import org.apache.http.HttpHost;
//import org.apache.http.client.config.RequestConfig;
//import org.apache.http.message.BasicHeader;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * 用于支持es集群配置 认证配置
// */
//@Configuration
//public class RestClientConfig  {
//    @Value("${es.iphosts:localhost:9200}")
//    private String ipHosts;
//    @Value("${es.password:null}")
//    private String password;
//    @Value("${es.username:null}")
//    private String username;
//    @Bean(destroyMethod = "close")
//    public RestHighLevelClient restHighLevelClient() {
//        List<HttpHost> hosts = new ArrayList<>();
//        String[] iphostArray = ipHosts.split(";");
//        for (int i = 0; i < iphostArray.length; i++) {
//            if (StringUtils.isEmpty(iphostArray[i])) {
//                continue;
//            }
//            hosts.add(new HttpHost(iphostArray[i].split(":")[0],
//                    Integer.valueOf(iphostArray[i].split(":")[1]),"http"));
//        }
//        Header[] headers = new BasicHeader[2];
//        headers[0] = new BasicHeader("username",username);
//        headers[1] = new BasicHeader("password",password);
//        RestClientBuilder builder = RestClient.builder(hosts.stream().toArray(HttpHost[]::new))
//                .setDefaultHeaders(headers)
//                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
//                    @Override
//                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
//                        return builder.setConnectTimeout(10000 * 1000)
//                                /** 套接字超时（默认为30秒）//更改客户端的超时限制默认30秒现在改为100*1000分钟 */
//                                .setSocketTimeout(10000 * 1000);
//                    }
//                });
//        RestHighLevelClient client =  new RestHighLevelClient(builder);
//        return client;
//    }
//}
